package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.ReceiptManagement;
import cn.wolfcode.crm.mapper.ReceiptManagementMapper;
import cn.wolfcode.crm.query.ReceiptManagementQueryObj;
import cn.wolfcode.crm.service.IReceiptManagementService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReceiptManagementServiceImpl implements IReceiptManagementService {

    @Autowired
    private ReceiptManagementMapper receiptManagementMapper;

    public void delete(Long id) {
        receiptManagementMapper.deleteByPrimaryKey(id);
    }

    public void save(ReceiptManagement entity) {
        if(entity != null){
            //设置审核状态
            entity.setReviewStatus(ReceiptManagement.NO_AUDIT);
            receiptManagementMapper.insert(entity);
        }
    }

    public void update(ReceiptManagement entity) {
        //判断当前是否已经审核了  只有没有审核才允许更新操作
        ReceiptManagement receiptManagement = receiptManagementMapper.selectByPrimaryKey(entity.getId());
        if(receiptManagement.getReviewStatus() != ReceiptManagement.NO_AUDIT){
            throw new RuntimeException("已审核 不允许操作");
        }
        receiptManagementMapper.updateByPrimaryKey(entity);
    }

    public ReceiptManagement get(Long id) {
        return receiptManagementMapper.selectByPrimaryKey(id);
    }

    public List<?> list() {
        return receiptManagementMapper.selectAll();
    }

    public PageResult query(ReceiptManagementQueryObj qo) {

        //查总数
        int total = receiptManagementMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        //查数据
        List<ReceiptManagement> rows = receiptManagementMapper.queryForList(qo);
        return new PageResult(total, rows);
    }
    //审核
    public void audit(Long id) {
        //判断当前是否已经审核了  只有没有审核才允许更新操作
        ReceiptManagement receiptManagement = receiptManagementMapper.selectByPrimaryKey(id);
        if(receiptManagement.getReviewStatus() != ReceiptManagement.NO_AUDIT){
            throw new RuntimeException("已审核 不允许操作");
        }
        receiptManagement.setReviewStatus(ReceiptManagement.AUDIT);
        receiptManagementMapper.audit(receiptManagement);
    }
}
